var express = require('express');
var router = express.Router();
//获取 json 文件的数据
let data = require('../data/news.json');

/**
 * 客户端渲染
 */
//接口
router.get('/api', function(req, res, next) {
  //响应 JSON
  res.json(data);
});

//声明一个路由规则
router.get('/home', (req, res) => {
  //响应一个网页
  res.render('home');
});

/**
 * 服务端渲染
 */
router.get('/home2', (req, res) => {
  res.send(`
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
    </head>
    <body>
      <h2>${data.title}</h2>
      <img src="${data.img}" />
    </body>
    </html>  
  `);
});


module.exports = router;
